package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("/shopping")
@RestController
public class ZOrderController {
    @Reference(timeout = 3000, check = false)
    OrderQueryService orderQueryService;

    @Reference(timeout = 3000, check = false)
    OrderCoreService orderCoreService;

    @GetMapping("/order")
    public ResponseData order(OrderListRequest listRequest, HttpServletRequest request) {
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        OrderListResponse response = orderQueryService.order(uid, listRequest.getPage(), listRequest.getSize());
        return new ResponseUtil().setData(response);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/order/{id}")
    public ResponseData orderDetail(@PathVariable("id") String id,HttpServletRequest httpRequest) {
        String userInfo = (String) httpRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());
        String username =object.get("username").toString();

        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(id);

        OrderDetailResponse orderDetailResponse = orderQueryService.orderDetail(request);
        orderDetailResponse.setUserName(username);
        orderDetailResponse.setUserId(uid);
        if(orderDetailResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(orderDetailResponse);
        }
        return new ResponseUtil<>().setErrorMsg(orderDetailResponse.getMsg());
    }

    /**
     * 取消订单
     * @param
     * @return
     */
    @PostMapping("/cancelOrder")
    public ResponseData orderCancle(@RequestBody CancelOrderRequest request) {
        CancelOrderResponse cancelOrderResponse = orderCoreService.orderCancle(request);
        return new ResponseUtil<>().setData(cancelOrderResponse.getMsg());
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/order/{id}")
    public ResponseData orderDel(@PathVariable("id") String id) {
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(id);
        request.setOrderId(id);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.orderDel(request);
        return new ResponseUtil<>().setData(deleteOrderResponse.getMsg());
    }
}
